/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJPA;

import controladoresJPA.exceptions.NonexistentEntityException;
import controladoresJPA.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import modelo.Ingreso;
import modelo.TransferenciaIngreso;
import modelo.Usuario;

/**
 *
 * @author Francisco
 */
public class TransferenciaIngresoJpaController implements Serializable {

    public TransferenciaIngresoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TransferenciaIngreso transferenciaIngreso) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Ingreso idIngreso = transferenciaIngreso.getIdIngreso();
            if (idIngreso != null) {
                idIngreso = em.getReference(idIngreso.getClass(), idIngreso.getIdIngreso());
                transferenciaIngreso.setIdIngreso(idIngreso);
            }
            Usuario idUsuarioDestino = transferenciaIngreso.getIdUsuarioDestino();
            if (idUsuarioDestino != null) {
                idUsuarioDestino = em.getReference(idUsuarioDestino.getClass(), idUsuarioDestino.getIdUsuario());
                transferenciaIngreso.setIdUsuarioDestino(idUsuarioDestino);
            }
            em.persist(transferenciaIngreso);
            if (idIngreso != null) {
                idIngreso.getTransferenciaIngresoList().add(transferenciaIngreso);
                idIngreso = em.merge(idIngreso);
            }
            if (idUsuarioDestino != null) {
                idUsuarioDestino.getTransferenciaIngresoList().add(transferenciaIngreso);
                idUsuarioDestino = em.merge(idUsuarioDestino);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TransferenciaIngreso transferenciaIngreso) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            TransferenciaIngreso persistentTransferenciaIngreso = em.find(TransferenciaIngreso.class, transferenciaIngreso.getIdTransferenciaIngreso());
            Ingreso idIngresoOld = persistentTransferenciaIngreso.getIdIngreso();
            Ingreso idIngresoNew = transferenciaIngreso.getIdIngreso();
            Usuario idUsuarioDestinoOld = persistentTransferenciaIngreso.getIdUsuarioDestino();
            Usuario idUsuarioDestinoNew = transferenciaIngreso.getIdUsuarioDestino();
            if (idIngresoNew != null) {
                idIngresoNew = em.getReference(idIngresoNew.getClass(), idIngresoNew.getIdIngreso());
                transferenciaIngreso.setIdIngreso(idIngresoNew);
            }
            if (idUsuarioDestinoNew != null) {
                idUsuarioDestinoNew = em.getReference(idUsuarioDestinoNew.getClass(), idUsuarioDestinoNew.getIdUsuario());
                transferenciaIngreso.setIdUsuarioDestino(idUsuarioDestinoNew);
            }
            transferenciaIngreso = em.merge(transferenciaIngreso);
            if (idIngresoOld != null && !idIngresoOld.equals(idIngresoNew)) {
                idIngresoOld.getTransferenciaIngresoList().remove(transferenciaIngreso);
                idIngresoOld = em.merge(idIngresoOld);
            }
            if (idIngresoNew != null && !idIngresoNew.equals(idIngresoOld)) {
                idIngresoNew.getTransferenciaIngresoList().add(transferenciaIngreso);
                idIngresoNew = em.merge(idIngresoNew);
            }
            if (idUsuarioDestinoOld != null && !idUsuarioDestinoOld.equals(idUsuarioDestinoNew)) {
                idUsuarioDestinoOld.getTransferenciaIngresoList().remove(transferenciaIngreso);
                idUsuarioDestinoOld = em.merge(idUsuarioDestinoOld);
            }
            if (idUsuarioDestinoNew != null && !idUsuarioDestinoNew.equals(idUsuarioDestinoOld)) {
                idUsuarioDestinoNew.getTransferenciaIngresoList().add(transferenciaIngreso);
                idUsuarioDestinoNew = em.merge(idUsuarioDestinoNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = transferenciaIngreso.getIdTransferenciaIngreso();
                if (findTransferenciaIngreso(id) == null) {
                    throw new NonexistentEntityException("The transferenciaIngreso with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            TransferenciaIngreso transferenciaIngreso;
            try {
                transferenciaIngreso = em.getReference(TransferenciaIngreso.class, id);
                transferenciaIngreso.getIdTransferenciaIngreso();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The transferenciaIngreso with id " + id + " no longer exists.", enfe);
            }
            Ingreso idIngreso = transferenciaIngreso.getIdIngreso();
            if (idIngreso != null) {
                idIngreso.getTransferenciaIngresoList().remove(transferenciaIngreso);
                idIngreso = em.merge(idIngreso);
            }
            Usuario idUsuarioDestino = transferenciaIngreso.getIdUsuarioDestino();
            if (idUsuarioDestino != null) {
                idUsuarioDestino.getTransferenciaIngresoList().remove(transferenciaIngreso);
                idUsuarioDestino = em.merge(idUsuarioDestino);
            }
            em.remove(transferenciaIngreso);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TransferenciaIngreso> findTransferenciaIngresoEntities() {
        return findTransferenciaIngresoEntities(true, -1, -1);
    }

    public List<TransferenciaIngreso> findTransferenciaIngresoEntities(int maxResults, int firstResult) {
        return findTransferenciaIngresoEntities(false, maxResults, firstResult);
    }

    private List<TransferenciaIngreso> findTransferenciaIngresoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TransferenciaIngreso.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TransferenciaIngreso findTransferenciaIngreso(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TransferenciaIngreso.class, id);
        } finally {
            em.close();
        }
    }

    public int getTransferenciaIngresoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TransferenciaIngreso> rt = cq.from(TransferenciaIngreso.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
